package com.CTAtracker.android.DataBase;

import android.content.ContentValues;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteException;

/**
 * This class is an helper for the connection to the database for the table favorite
 */
public class MySQLiteHelperFavorite extends MySQLiteHelper {

	/**
	 * The name of the table favorite
	 */
	public static final String TABLE_FAVORITE = "favorites";
	/**
	 * The name of the database field : idFavorite
	 */
	public static final String FAVORITE_ID = "_idFavorite";
	/**
	 * The name of the database field : nameStop
	 */
	public static final String FAVORITE_NAME_STOP = "nameStop";
	/**
	 * The name of the database field : idStop
	 */
	public static final String FAVORITE_ID_STOP = "idStop";
	/**
	 * The name of the database field : idParent
	 */
	public static final String FAVORITE_ID_PARENT = "idParent";
	/**
	 * The id of the line 
	 */
	public static final String FAVORITE_ID_LINE = "idLine";
	/**
	 * The query to create the table favorite in the database 
	 */
	private static final String DATABASE_CREATE_FAVORITE = "create table " + TABLE_FAVORITE 
			+ " (" + FAVORITE_ID 	+ " integer primary key autoincrement, " 
			+ FAVORITE_NAME_STOP 	+ " text not null, " 
			+ FAVORITE_ID_STOP 		+ " integer not null," 
			+ FAVORITE_ID_PARENT 	+ " integer not null,"
			+ FAVORITE_ID_LINE 		+ " integer not null);";
	
	public static String[] allColumns = { 
		FAVORITE_ID,
		FAVORITE_ID_STOP,
		FAVORITE_NAME_STOP, 
		FAVORITE_ID_PARENT, 
		FAVORITE_ID_LINE};

	/**
	 * Constructor, instantiate the database with the method super 
	 * @param context the current context 
	 */
	public MySQLiteHelperFavorite(Context context) {
		super(context);

	}

	/**
	 * This method is called the first time we create the database
	 */
	@Override
	public void onCreate(SQLiteDatabase db) {
		MySQLiteHelperLine.createData(db); 
		MySQLiteHelperStation.createData(db);
		MySQLiteHelperFavorite.createData(db);
	}

	/**
	 * Create the query to insert a new favorite in the database from values
	 * @param database the database to insert the new line
	 * @param name the name of the line 
	 * @param stop_id the Id of the stop of the favorite
	 * @throws SQLiteException 
	 */
	public void createFavorite(SQLiteDatabase database, 	
			String name, int stop_id, int parent_id, int idLine) throws SQLiteException {

		ContentValues values = new ContentValues();

		values.put(MySQLiteHelperFavorite.FAVORITE_NAME_STOP, name);
		values.put(MySQLiteHelperFavorite.FAVORITE_ID_STOP, stop_id);
		values.put(MySQLiteHelperFavorite.FAVORITE_ID_PARENT, parent_id);
		values.put(MySQLiteHelperFavorite.FAVORITE_ID_LINE, idLine);
		if (database.insert(TABLE_FAVORITE, null, values) == -1)
			throw new SQLiteException();
	}

	/**
	 * Create the query to delete a favorite from the database
	 * @param database the database to delete the favorite
	 * @param idFavorite the Id of the favorite stop to delete
	 */
	public void deleteFavorite(SQLiteDatabase database, int idStop){
		database.delete(TABLE_FAVORITE, FAVORITE_ID_STOP + "=" + idStop, null);
	}
	
	
	/**
	 * Called by the method onCreate. Create in the database the table favorite
	 * @param db the database to create the data in
	 */
	public static void createData(SQLiteDatabase db) {
		db.execSQL(DATABASE_CREATE_FAVORITE);
	}
}
